using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Data;
using System.Diagnostics;
using System.IO;

namespace OrderSystem.NPOIOperateExcel
{
	public class ExportShatterList
	{
		private HSSFWorkbook hssfworkbook;

		private FileStream fs;

		private bool OpenFileAfterSaved;

		private bool _isDisposed = false;

		public ExportShatterList(string templetFile, bool bOpenFileAfterSaved)
		{
			FileStream fileStream = File.Open(templetFile, FileMode.Open);
			this.hssfworkbook = new HSSFWorkbook(fileStream);
			this.OpenFileAfterSaved = bOpenFileAfterSaved;
			fileStream.Close();
			fileStream.Dispose();
		}

		public void MakeDataToExcel(string sSavePath, DataTable dt)
		{
			ISheet sheetAt = this.hssfworkbook.GetSheetAt(0);
			IRow row = sheetAt.CreateRow(0);
			for (int i = 0; i < dt.Columns.Count; i++)
			{
				row.CreateCell(i).SetCellValue(dt.Columns[i].ColumnName);
			}
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				IRow row2 = sheetAt.CreateRow(i + 1);
				for (int j = 0; j < dt.Columns.Count; j++)
				{
					row2.CreateCell(j).SetCellValue(dt.Rows[i][j].ToString());
				}
			}
			using (this.fs = File.OpenWrite(sSavePath))
			{
				this.hssfworkbook.Write(this.fs);
				this.fs.Close();
				this.fs.Dispose();
				this.hssfworkbook.Clear();
				if (this.OpenFileAfterSaved)
				{
					Process.Start(sSavePath);
				}
			}
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		protected virtual void Dispose(bool disposing)
		{
			if (!this._isDisposed)
			{
				if (disposing)
				{
					if (this.hssfworkbook != null)
					{
						this.hssfworkbook.Clear();
					}
					if (this.fs != null)
					{
						this.fs.Close();
						this.fs.Dispose();
					}
				}
				this._isDisposed = true;
			}
		}

		~ExportShatterList()
		{
			this.Dispose(false);
		}
	}
}
